package xxh;

import javax.swing.*;
import java.awt.*;
import java.sql.*;
import java.util.HashMap;

class ReturnUI extends JPanel {
    private Connection connection;
    JLabel lblUserNo;
    JTextField txtUserNo;
    JLabel lblBookNo;
    JComboBox<String> cmbBookNo;
    JButton btnSubmit;
    JLabel lblLendDate;
    JTextField txtLendDate;

    public ReturnUI(String Uno) throws SQLException {
        setLayout(new BorderLayout()); // 使用 BorderLayout 布局

        // 增大字体
        Font largeFont = new Font("宋体", Font.BOLD, 18);

        // 创建上部面板，放置用户编号和书籍信息
        JPanel topPanel = new JPanel();
        topPanel.setLayout(new GridLayout(3, 2, 10, 10));  // 使用 GridLayout 布局来排列组件
        topPanel.setBorder(BorderFactory.createEmptyBorder(50, 20, 30, 20)); // 设置面板的边距

        lblUserNo = new JLabel("用户编号:");
        lblUserNo.setFont(largeFont);
        txtUserNo = new JTextField();
        txtUserNo.setText(Uno);
        txtUserNo.setEditable(false);
        txtUserNo.setFont(new Font("宋体", Font.PLAIN, 18));
        txtUserNo.setBackground(Color.LIGHT_GRAY);

        lblBookNo = new JLabel("书籍:");
        lblBookNo.setFont(largeFont);
        cmbBookNo = new JComboBox<>();
        cmbBookNo.setFont(new Font("宋体", Font.PLAIN, 18));

        lblLendDate = new JLabel("归还时间:");
        lblLendDate.setFont(largeFont);
        txtLendDate = new JTextField(java.time.LocalDate.now().toString());
        txtLendDate.setEditable(false);
        txtLendDate.setFont(new Font("宋体", Font.PLAIN, 18));
        txtLendDate.setBackground(Color.LIGHT_GRAY);

        // 将各组件添加到 topPanel 中
        topPanel.add(lblUserNo);
        topPanel.add(txtUserNo);
        topPanel.add(lblBookNo);
        topPanel.add(cmbBookNo);
        topPanel.add(lblLendDate);
        topPanel.add(txtLendDate);

        // 创建按钮面板，用于放置还书按钮
        JPanel bottomPanel = new JPanel();
        bottomPanel.setLayout(new FlowLayout(FlowLayout.CENTER, 20, 10)); // 中心对齐，设置间距
        btnSubmit = new JButton("还书");
        btnSubmit.setFont(new Font("宋体", Font.BOLD, 18));
        btnSubmit.setBackground(new Color(50, 150, 250));
        btnSubmit.setForeground(Color.WHITE);
        btnSubmit.setFocusPainted(false);
        btnSubmit.setBorder(BorderFactory.createLineBorder(new Color(50, 150, 250), 2));
        btnSubmit.setPreferredSize(new Dimension(200, 50));
        bottomPanel.add(btnSubmit);


        add(topPanel, BorderLayout.NORTH);
        add(bottomPanel, BorderLayout.SOUTH);

        // 处理还书事件
        ReturnProcess rtp = new ReturnProcess(cmbBookNo, Uno);
        btnSubmit.addActionListener(e -> {
            String userNo = txtUserNo.getText();
            String bookNo = (String) cmbBookNo.getSelectedItem();
            try {
                if (rtp.returnBook(userNo, bookNo)) {
                    JOptionPane.showMessageDialog(this, "归还成功！", "Success", JOptionPane.INFORMATION_MESSAGE);
                } else {
                    JOptionPane.showMessageDialog(this, "归还失败！", "Error", JOptionPane.ERROR_MESSAGE);
                }
            } catch (SQLException ex) {
                throw new RuntimeException(ex);
            }
        });
    }
}
